<?php
include_once dirname(__FILE__).'/Rectangle.class.php';
include_once dirname(__FILE__).'/Transform.class.php';

class Image {
	private $_width;
	private $_height;
	private $_idest;
	
	public function createImage($width, $height){
		$this->_idest = imagecreate($width, $height);
		$this->_width = $width;
		$this->_height = $height;
	}
	
	/**
	 * Function for load images, with/without croupp
	 * 
	 * @param string $src	Original image file
	 * @param int $width	New width
	 * @param int $height	New height
	 * @param boolean $crop Crop true/false
	 * @return if complete return imagecreatetruecolor()
	 */	
	public function loadImage($src, $width, $height, $cropp = true, $centred = true){
		if (!file_exists($src)) return false;
		$this->_idest = self::imgResize($src,$width, $height, $cropp, $centred);
		return true;
	}
	
	/**
	 * Function for resize images, without loss of quality, and with/without croup
	 * 
	 * @param string $src	Original image file
	 * @param int $width	New width
	 * @param int $height	New height
	 * @param boolean $crop Crop true/false
	 * @return if complete return imagecreatetruecolor()
	 */
	private function imgResize($src, $width, $height, $cropp = true, $centred = true){		
		$imageInfo = new ImageInfo($src);
		$isrc = $imageInfo->image;
		
		$rectangle = Transform::putInSpace($imageInfo->size, array($width,$height), $cropp, $centred);
		
		if ($centred == false && $cropp == false){
			$idest2 = imagecreatetruecolor($rectangle->width, $rectangle->height);
			$this->_width = $rectangle->width;
			$this->_height = $rectangle->height;
		} else {
			$idest2 = imagecreatetruecolor($width, $height);
			$this->_width = $width;
			$this->_height = $height;
		}
		
		imagecopyresampled(	$idest2, $isrc, 
							$rectangle->x, $rectangle->y, 0, 0, 
							$rectangle->width, $rectangle->height, 
							$imageInfo->size[0], $imageInfo->size[1]);
		
		imagedestroy($isrc);
		return $idest2;
	}
	
	public function add($src, $cropp = false) {
		$imageInfo = new ImageInfo($src);
		$isrc = $imageInfo->image;
		
		$rectangle = Transform::putInSpace($imageInfo->size, array($this->_width, $this->_height), $cropp, true);
		
		imagecopyresampled(	$this->_idest, $isrc, 
							$rectangle->x, $rectangle->y, 0, 0, 
							$rectangle->width, $rectangle->height, 
							$imageInfo->size[0], $imageInfo->size[1]);
		
		imagedestroy($isrc);
	}
	
	public function displayImage($quality = 95){
		imagejpeg($this->_idest, '', $quality);
	}
	
	public function saveImage($fileName, $quality = 95) {
		imagejpeg($this->_idest, $fileName, $quality);
	}
	
	function __destruct(){
		imagedestroy($this->_idest);
	}
}

class ImageInfo{
	public $size;
	public $image;
	
	function __construct($imagePath){
		$this->size = getimagesize($imagePath);
		
		if ($this->size === false) return false;		
		
		$mime = $this->size['mime'];
		$format = strtolower(substr($mime, strpos($mime, '/')+1));
		$icfunc = "imagecreatefrom" . $format;
		
		if (!function_exists($icfunc)) return false;
		
		$this->image = $icfunc($imagePath);
	}
}
?>